
# Proposition 26 

# Data preparation ----

gc()

## Set wd and load data
setwd("")
load("panel_us.RData")

## Load packages
library(sjmisc)
library(tidyverse)
source("function_plot_agr.R")

# Take a glimpse, select variables of interest, remove labels, frequencies
glimpse(cali)

df <- cali %>% select(vint_prop26_w2, vchoice_prop26_w2, particip_w2,
                      party_cue, pid_close_w1, pid_first_w1, pid_remind_w1, pid_remind_first_w1,
                      imp_prop26_w2, complex_prop26_w2,
                      genknow_a_w1, genknow_b_w1, genknow_c_w1, genknow_d_w1,  
                      arg_prop26_a_w2, arg_prop26_b_w2, arg_prop26_c_w2,
                      arg_prop26_d_w2, arg_prop26_e_w2, arg_prop26_f_w2, 
                      party_reco_prop26_w2, gov_reco_prop26_w2, 
                      intgrp_cniga_reco_prop26_w2, intgrp_cta_reco_prop26_w2,
                      intgrp_tribe_reco_prop26_w2, intgrp_drking_reco_prop26_w2,
                      day_itw_w2, month_itw_w2, isknow_prop26_a_w2, isknow_prop26_b_w2, 
                      starts_with("att"), -att_campaign_w3,
                      gender, age, educ_w1, county_w1, received_w2, already_vote_w2)
                      
df <- labelled::remove_labels(df)


## Regions ----
assign_region <- function(county) {
  southern_counties <- c("RIVERSIDE", "SAN DIEGO", "LOS ANGELES", "SAN BERNARDINO", "ORANGE", "VENTURA", "KERN", "IMPERIAL")
  
  if (county %in% southern_counties) {
    return("Southern California")
  } else {
    return("Northern California")
  }
}

df$region <- sapply(df$county_w1, assign_region)

df %>% select(county_w1, region) %>% print(n=500)
frq(df$region)

rm(assign_region)

## Gender ----
frq(df$gender)
df <- df %>% filter(gender == "male" | gender == "female")
df$gender <- droplevels(df$gender)
frq(df$gender)


## Vote intention/choice ----
frq(df$vint_prop26_w2)
frq(df$vchoice_prop26_w2)

df <- df %>%
  mutate(votew2 = case_when(
    vint_prop26_w2 == "certainly favor" | vint_prop26_w2 == "likely favor" | vchoice_prop26_w2 == "yes" ~ "yes",
    vint_prop26_w2 == "certainly against" | vint_prop26_w2 == "likely against" | vchoice_prop26_w2 == "no" ~ "no",
    vint_prop26_w2 == "dk" | vchoice_prop26_w2 == "dk" ~ "dk"),
         vote_w2_int_choice = case_when(
    vint_prop26_w2 == "certainly favor" | vint_prop26_w2 == "likely favor" ~ "int yes",
    vchoice_prop26_w2 == "yes" ~ "choice yes",
    vint_prop26_w2 == "certainly against" | vint_prop26_w2 == "likely against" ~ "int no",
    vchoice_prop26_w2 == "no" ~ "choice no",
    vint_prop26_w2 == "dk" | vchoice_prop26_w2 == "dk" ~ "dk"))

frq(df$votew2)
xtabs(~ vint_prop26_w2 + votew2, data = df, subset = NULL)
xtabs(~ vchoice_prop26_w2 + votew2, data = df, subset = NULL)

frq(df$vote_w2_int_choice)
xtabs(~ vint_prop26_w2 + vote_w2_int_choice, data = df, subset = NULL)
xtabs(~ vchoice_prop26_w2 + vote_w2_int_choice, data = df, subset = NULL)



## Merge day / week of itw ----
frq(df$day_itw_w2)
frq(df$month_itw_w2)

xtabs(~ day_itw_w2 + month_itw_w2, data = df, subset = NULL)

df <- df %>% mutate(date_itw = paste(as.character(day_itw_w2), as.character(month_itw_w2), "2022", sep = "-"))

df$date_itw <- as.Date(df$date_itw, format = "%d-%m-%Y")

frq(df$date_itw)

df <- df %>% mutate(days = 41-(as.numeric(difftime(as.Date("2022-11-08"), date_itw, units = "days"))))

frq(df$days)
xtabs(~ date_itw + days, data = df, subset = NULL)

### Number of observations / each day 
freq_table <- as.data.frame(table(df$days, useNA = "no"))
N_values <- freq_table$Freq
mean(N_values)
sd(N_values)

rm(freq_table,N_values)



## Already voted or not
xtabs(~ already_vote_w2 + received_w2, data = df, addNA = T, subset = NULL)

df <- df %>%
  mutate(received_w2 = case_when(
    received_w2 == 1 ~ "yes",
    received_w2 == 2 ~ "no",
    received_w2 == 99 ~ "nsp",
    TRUE ~ NA), 
    already_vote_w2 = case_when(
      already_vote_w2 == 1 ~ "yes",
      already_vote_w2 == 2 ~ "no",
      TRUE ~ NA),
    votemail = case_when(
      received_w2 %in% c("no","nsp") ~ "not receive",
      received_w2 == "yes" & already_vote_w2 == "no" ~ "receive not vote",
      received_w2 == "yes" & already_vote_w2 == "yes" ~ "receive and vote",
      TRUE ~ NA))

xtabs(~ already_vote_w2 + received_w2, data = df, addNA = T, subset = NULL)
xtabs(~ already_vote_w2 + votemail, data = df, addNA = T, subset = NULL)
xtabs(~ votemail + received_w2, data = df, addNA = T, subset = NULL)
frq(df$votemail)
xtabs(~ votemail + vote_w2_int_choice, data = df, addNA = T, subset = NULL)

df <- df %>% mutate(filter_particip_profile = 
                      case_when(votemail == "receive and vote" | particip_w2 == "certainly yes" ~ 1,
                                is.na(days) ~ NA,
                                TRUE ~ 0))
frq(df$filter_particip_profile)
df %>% select(days, received_w2, already_vote_w2, particip_w2, votemail, vote_w2_int_choice, votew2, filter_particip_profile) %>% print(n = 50)





## Arguments ----
col_names <- c("arg_prop26_a_w2", "arg_prop26_b_w2", "arg_prop26_c_w2", "arg_prop26_d_w2", "arg_prop26_e_w2", "arg_prop26_f_w2")

df <- df %>%
  mutate(across(c(arg_prop26_a_w2, arg_prop26_b_w2, arg_prop26_c_w2),
                  ~ case_when(. == "strong agree" ~ 2, . == "agree" ~ 1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ -1, . == "strong disagree" ~ -2),
                              .names = "{col}_rcd")) %>% 
  mutate(across(c(arg_prop26_d_w2, arg_prop26_e_w2, arg_prop26_f_w2),
                  ~ case_when(. == "strong agree" ~ -2, . == "agree" ~ -1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ 1, . == "strong disagree" ~ 2),
                              .names = "{col}_rcd")) %>% 
  mutate(arg_pro_prop26_w2 = arg_prop26_a_w2_rcd + arg_prop26_b_w2_rcd + arg_prop26_c_w2_rcd,
         arg_con_prop26_w2 = arg_prop26_d_w2_rcd + arg_prop26_e_w2_rcd + arg_prop26_f_w2_rcd,
         arg_full_prop26_w2 = arg_pro_prop26_w2 + arg_con_prop26_w2) %>% 
  mutate(arg_n_dk = case_when(rowSums(!is.na(across(all_of(col_names)))) == 0 ~ NA_real_,
                              T ~ rowSums(across(all_of(col_names)) == "dk", na.rm = T)))

rm(col_names)


## PID ----
df %>% select(starts_with("pid")) %>% frq()
xtabs(~ pid_close_w1 + pid_first_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_remind_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_first_w1 + pid_remind_first_w1, data = df, subset = NULL)


df <- df %>% mutate(pid = 
                      case_when(pid_first_w1 == "Democrat" | pid_remind_first_w1 == "Democrat" ~ "Democrat",
                                pid_first_w1 == "Republican" | pid_remind_first_w1 == "Republican" ~ "Republican",
                                pid_remind_first_w1 == "dk" ~ "noPID",
                                pid_close_w1 == "no" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "dk" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "yes" & pid_remind_w1 == "no" ~ "noPID"))

frq(df$pid)
df %>% select(starts_with("pid")) %>% print(n=50)


## Appendix A ----
dfsample1 <- df

frq(dfsample1$gender)
frq(dfsample1$age)
frq(dfsample1$educ_w1)

dfsample1 <- dfsample1 %>% mutate(age_rcd = case_when(age >= 18 & age <= 24 ~ "18-24",
                                                      age >= 25 & age <= 34 ~ "25-34",
                                                      age >= 35 & age <= 44 ~ "35-44",
                                                      age >= 45 & age <= 54 ~ "45-54",
                                                      age >= 55 & age <= 65 ~ "55-65",
                                                      age >= 66 ~ "66+"),
                                  educ_rcd = case_when(educ_w1 %in% c("grade<4","grade5-8","grade9-11","grade12(no diploma)","high school","GED", "college <1 year","college >1 year no degree") ~ "notuni",
                                                       educ_w1 %in% c("associate degree","bachelor","master","pro degree","PhD") ~ "uni"))

dfsample2 <- dfsample1 %>% drop_na(date_itw)

frq(dfsample1$gender)
frq(dfsample2$gender)

frq(dfsample1$age_rcd)
frq(dfsample2$age_rcd)

frq(dfsample1$educ_rcd)
frq(dfsample2$educ_rcd)

rm(dfsample1, dfsample2)


###


## Knowledge party cues ----
frq(df$pid)
df <- df %>% filter(pid %in% c("Democrat","Republican","noPID"))
frq(df$pid)

frq(df$party_reco_prop26_w2) 
xtabs(~ pid + party_reco_prop26_w2, data = df, subset = NULL)
addmargins(xtabs(~ pid + party_reco_prop26_w2, data = df, subset = NULL))

df <- df %>% 
  mutate(party_reco_prop26_w2_know = case_when(
    pid == "Republican" & party_reco_prop26_w2 == "no" ~ "correct",
    pid == "Democrat" & party_reco_prop26_w2 == "free" ~ "correct",
    pid == "Republican" & party_reco_prop26_w2 %in% c("yes","free","dk") ~ "not",
    pid == "Democrat" & party_reco_prop26_w2 %in% c("yes","no","dk") ~ "not",
    pid == "noPID" ~ "noPID"))

frq(df$party_reco_prop26_w2_know) 

# Group "free" with "dk" (knowledge of cues)
df <- df %>% 
  mutate(across(c(party_reco_prop26_w2),
                ~ case_when(. == "yes" ~ "yes",
                            . == "no" ~ "no",
                            . == "free" | . == "dk" ~ "free/dk"),
                .names = "{col}_rcd"))

frq(df$party_reco_prop26_w2_rcd)


## Importance ----
frq(df$imp_prop26_w2)
df$imp_prop26_w2[df$imp_prop26_w2 == 99] <- NA
frq(df$imp_prop26_w2)

df <- df %>% mutate(imp_prop26_w2_bi = case_when(
  imp_prop26_w2 < 6 ~ 0,
  imp_prop26_w2 >= 6 ~ 1,
  TRUE ~ NA))  

xtabs(~ imp_prop26_w2 + imp_prop26_w2_bi, data = df, subset = NULL)



## IS knowledge ----
frq(df$isknow_prop26_a_w2)
frq(df$isknow_prop26_b_w2)

df <- df %>%
  mutate(isknow_prop26_a_w2_rcd = ifelse(is.na(isknow_prop26_a_w2), NA, as.numeric(grepl("correct", tolower(isknow_prop26_a_w2)))),
         isknow_prop26_b_w2_rcd = ifelse(is.na(isknow_prop26_b_w2), NA, as.numeric(grepl("correct", tolower(isknow_prop26_b_w2)))))

frq(df$isknow_prop26_a_w2_rcd)
frq(df$isknow_prop26_b_w2_rcd)

df <- df %>% mutate(isknow_prop26_scale = isknow_prop26_a_w2_rcd + isknow_prop26_b_w2_rcd) %>% 
             mutate(isknow_prop26_scale_bi = case_when(isknow_prop26_scale == 0 ~ 0,
                                                       isknow_prop26_scale == 1 | isknow_prop26_scale == 2 ~ 1))
             
frq(df$isknow_prop26_scale)
frq(df$isknow_prop26_scale_bi)




## Select var and remove NA ----

df <- df %>% 
  select(isknow_prop26_scale, party_reco_prop26_w2_know, arg_n_dk, arg_full_prop26_w2, votemail, filter_particip_profile, 
         date_itw, days, votew2, pid, imp_prop26_w2, gender, age, educ_w1,
         arg_prop26_a_w2_rcd, arg_prop26_b_w2_rcd, arg_prop26_c_w2_rcd, arg_prop26_d_w2_rcd, arg_prop26_e_w2_rcd, arg_prop26_f_w2_rcd) %>% 
  drop_na()


## Recode mediators/outcomes ----

### Consistent voting ----
df <- df %>% mutate(
  voteinlinearg = case_when(votew2 == "dk" ~ "undecided",
                            arg_full_prop26_w2 <= -3 & votew2 == "no" ~ "inline",
                            arg_full_prop26_w2 <= -3 & votew2 == "yes" ~ "not inline",
                            arg_full_prop26_w2 >= -2 & arg_full_prop26_w2 <= 2 ~ "ambivalent",
                            arg_full_prop26_w2 >= 3 & votew2 == "no" ~ "not inline",
                            arg_full_prop26_w2 >= 3 & votew2 == "yes" ~ "inline"),
  voteinlinearg0 = case_when(votew2 == "dk" ~ "undecided",
                             arg_full_prop26_w2 <= -1 & votew2 == "no" ~ "inline",
                             arg_full_prop26_w2 <= -1 & votew2 == "yes" ~ "not inline",
                             arg_full_prop26_w2 == 0 ~ "ambivalent",
                             arg_full_prop26_w2 >= 1 & votew2 == "no" ~ "not inline",
                             arg_full_prop26_w2 >= 1 & votew2 == "yes" ~ "inline"),
  voteinlinearg5 = case_when(votew2 == "dk" ~ "undecided",
                             arg_full_prop26_w2 <= -6 & votew2 == "no" ~ "inline",
                             arg_full_prop26_w2 <= -6 & votew2 == "yes" ~ "not inline",
                             arg_full_prop26_w2 >= -5 & arg_full_prop26_w2 <= 5 ~ "ambivalent",
                             arg_full_prop26_w2 >= 6 & votew2 == "no" ~ "not inline",
                             arg_full_prop26_w2 >= 6 & votew2 == "yes" ~ "inline"),
  voteinlinearg_rcd = case_when(voteinlinearg == "inline" ~ 1,
                                voteinlinearg %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg0_rcd = case_when(voteinlinearg0 == "inline" ~ 1,
                                 voteinlinearg0 %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg5_rcd = case_when(voteinlinearg5 == "inline" ~ 1,
                                 voteinlinearg5 %in% c("ambivalent","not inline","undecided") ~ 0))

### Knowledge cue and vote inline ----
df <- df %>% mutate(party_reco_prop26_w2_know_rcd = case_when(party_reco_prop26_w2_know == "correct" ~ 1,
                                                              party_reco_prop26_w2_know == "noPID" | party_reco_prop26_w2_know == "not" ~ 0),
                    voteinlineparty = case_when(pid == "Democrat" & votew2 == "yes" ~ "inline",
                                                pid == "Democrat" & votew2 == "no" ~ "inline",
                                                pid == "Democrat" & votew2 == "dk" ~ "undecided",
                                                pid == "Republican" & votew2 == "yes" ~ "not inline",
                                                pid == "Republican" & votew2 == "no" ~ "inline",
                                                pid == "Republican" & votew2 == "dk" ~ "undecided",
                                                pid == "noPID" ~ "noPID"),
                    voteinlineparty_rcd = case_when(voteinlineparty == "inline" ~ 1,
                                                    voteinlineparty %in% c("not inline", "undecided", "noPID") ~ 0))


## Filter only party identifiers ---- 
df <- df %>% filter(pid != "noPID")
frq(df$pid)


# Descriptive / appendix A ----

frq(df$votemail)

frq(df$party_reco_prop26_w2_know_rcd)

ggplot(df) + 
  aes(x = arg_full_prop26_w2) +
  geom_density(fill = "grey", alpha = 0.5) +
  theme_minimal() +
  scale_x_continuous(limits = c(-12, 12), breaks = seq(-12, 12, by = 2)) + 
  xlab("Proposition 26 - argument scale") +
  ylab("Density") 

ggsave("scale_prop26.png", scale = 1, width = 12, height = 6, units = "cm", dpi = 600)

avg_dk_prop26 <- mean(df$arg_n_dk)
round(avg_dk_prop26,2)
sd_dk_prop26 <- sd(df$arg_n_dk)
round(sd_dk_prop26,2)

frq(df$voteinlineparty_rcd)
frq(df$voteinlinearg_rcd)

addmargins(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL))
cross_prop26 <- as.data.frame(questionr::prop(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL)))
cross_prop26 <- cross_prop26 %>% mutate(ballot = "prop26")
cross_prop26


# Alpha arguments
df %>% 
  select(arg_prop26_a_w2_rcd, arg_prop26_b_w2_rcd, arg_prop26_c_w2_rcd) %>% 
  psych::alpha()  

df %>% 
  select(arg_prop26_d_w2_rcd, arg_prop26_e_w2_rcd, arg_prop26_f_w2_rcd) %>% 
  psych::alpha() 



# Analysis - aggregate ----

## 2a ----
cat_ols(df, "party_reco_prop26_w2_know_rcd", ballot = "prop26")

p01_prop26 <- graf

p01_prop26 <- p01_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "", title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                                                       values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p01_prop26

magr <- lm(percentage ~ days * party_reco_prop26_w2_know_rcd, data = dfagr)
summary(magr)

magr_know_cue <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "party_reco_prop26_w2_know_rcd1" = "Knowledge party cue",
                       "days:party_reco_prop26_w2_know_rcd1" = "Days × Knowledge party cue"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 



## 2b ----
frq(df$arg_n_dk)

num_ols(df, "arg_n_dk", ballot = "prop26")
p02_prop26 <- graf

p02_prop26 <- p02_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "", title = "Proposition 26") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p02_prop26

magr <- lm(avg ~ days, data = dfagr)
summary(magr)

magr_dk <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term, "(Intercept)" = "Intercept", "days" = "Days"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig)  




## 2c ----
frq(df$voteinlineparty_rcd)

cat_ols(df, "voteinlineparty_rcd", ballot = "prop26")
p04_prop26 <- graf

p04_prop26 <- p04_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p04_prop26

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)

magr_inline <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlineparty_rcd1" = "Inline",
                       "days:voteinlineparty_rcd1" = "Days × Inline"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 




## 2d ----
frq(df$voteinlinearg_rcd)

cat_ols(df, "voteinlinearg_rcd", ballot = "prop26")
p03_prop26 <- graf

p03_prop26 <- p03_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p03_prop26

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)

magr_cons <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlinearg_rcd1" = "Consistent voting",
                       "days:voteinlinearg_rcd1" = "Days × Consistent voting"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 



## Appendix B1 ---- 

writexl::write_xlsx(list(
  "2a" = magr_know_cue, "2b" = magr_dk,
  "2c" = magr_inline, "2d" = magr_cons),
  "magr_prop26.xlsx")

rm(graf, dfagr, magr)



# Analysis - individual ---- 

glimpse(df)

colsss <- c("gender", "voteinlineparty_rcd", "voteinlinearg_rcd", "voteinlinearg0_rcd", "voteinlinearg5_rcd")
df[colsss] <- lapply(df[colsss], as.factor)

colsss <- c("party_reco_prop26_w2_know_rcd", "isknow_prop26_scale", "arg_n_dk", "days", "imp_prop26_w2", "age", "educ_w1")
df[colsss] <- lapply(df[colsss], as.numeric)
df[colsss] <- lapply(df[colsss], scales::rescale)

rm(colsss)

## Appendix B2 ----

library(lavaan)

glimpse(df)

df$party_reco_prop26_w2_know_rcd <- as.factor(df$party_reco_prop26_w2_know_rcd)
df$gender <- as.numeric(df$gender)

#

model <- '
  # Days -> cue -> vote inline
  party_reco_prop26_w2_know_rcd ~ a1*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_prop26_w2_know_rcd + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlinearg_rcd ~ c2*days + b2*arg_n_dk + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_prop26_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'


fit_prop26 <- sem(model, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_prop26_w2_know_rcd"))

sumfit_prop26 <- summary(fit_prop26, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop26_df <- sumfit_prop26[["pe"]]



# Robustness ----

## Model for figure C3a.3 ----

model0 <- '
  # Days -> cue -> vote inline
  party_reco_prop26_w2_know_rcd ~ a1*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_prop26_w2_know_rcd + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlinearg0_rcd ~ c2*days + b2*arg_n_dk + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_prop26_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg0_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_prop260 <- sem(model0, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg0_rcd","party_reco_prop26_w2_know_rcd"))

sumfit_prop260 <- summary(fit_prop260, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop260_df <- sumfit_prop260[["pe"]]


## Model for figure C3b.3 ----

model5 <- '
  # Days -> cue -> vote inline
  party_reco_prop26_w2_know_rcd ~ a1*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_prop26_w2_know_rcd + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlinearg5_rcd ~ c2*days + b2*arg_n_dk + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_prop26_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg5_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_prop265 <- sem(model5, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg5_rcd","party_reco_prop26_w2_know_rcd"))

sumfit_prop265 <- summary(fit_prop265, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop265_df <- sumfit_prop265[["pe"]]



## Appendix C2b ----

### Figure C2b.2c ----

dfrep <- df %>% filter(pid == "Republican")

frq(dfrep$voteinlineparty_rcd)

cat_ols(dfrep, "voteinlineparty_rcd", ballot = "prop26")

prop26rep_04 <- graf

prop26rep_04[["data"]][["days"]] <- prop26rep_04[["data"]][["days"]] * 40

prop26rep_04 <- prop26rep_04 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

prop26rep_04
ggsave(filename = "prop26_onlyRep_agrrcs.png", units = "cm", width = 7, height = 8, dpi = 600)

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)

rm(graf)


### Figure C2a.3b ----

fit_prop26rep <- sem(model, data = dfrep, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_prop26_w2_know_rcd"))

summary(fit_prop26rep)

sumfit_fit_prop26rep <- summary(fit_prop26rep, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_fit_prop26rep_df <- sumfit_fit_prop26rep[["pe"]]

# Empty dataframe
med_summary <- data.frame(matrix(ncol = 7, nrow = 1))
x <- c("effect", "ind", "lowind", "uppind", "dir", "lowdir", "uppdir")
colnames(med_summary) <- x

# Outcome = vote inline party cue
med_summary[1,1] <- "Proposition 26 : Time > knowledge party cue > vote inline"
med_summary[1,2] <- sumfit_fit_prop26rep_df[sumfit_fit_prop26rep_df$label == "days_cue_inline", 6]
med_summary[1,3] <- sumfit_fit_prop26rep_df[sumfit_fit_prop26rep_df$label == "days_cue_inline", 10]
med_summary[1,4] <- sumfit_fit_prop26rep_df[sumfit_fit_prop26rep_df$label == "days_cue_inline", 11]
med_summary[1,5] <- sumfit_fit_prop26rep_df[sumfit_fit_prop26rep_df$label == "c1", 6]
med_summary[1,6] <- sumfit_fit_prop26rep_df[sumfit_fit_prop26rep_df$label == "c1", 10]
med_summary[1,7] <- sumfit_fit_prop26rep_df[sumfit_fit_prop26rep_df$label == "c1", 11]


med_summary


# Indirect
ind <- med_summary %>% dplyr::select(1:4)
ind

ind$effect <- factor(ind$effect, 
                     levels = c("Proposition 26 : Time > knowledge party cue > vote inline",
                     ordered = T))

ind_plot <- ggplot(ind, aes(x = effect, y = ind)) + 
  geom_pointrange(aes(ymin = lowind, ymax = uppind), size = 0.25) +
  scale_y_continuous(limits = c(-0.06,0.45), breaks = seq(-0.05, 0.40, by = 0.10)) +
  geom_hline(yintercept = 0) +
  xlab("") + ylab("Estimate") +
  labs(title = "Indirect") +
  theme_minimal() +
  coord_flip()  

ind_plot

ggsave("prop26_onlyRep_mediation.png", scale = 1, width = 18, height = 4, units = "cm", dpi = 600)



## Figure C3a.2d ----

frq(df$voteinlinearg0_rcd)

cat_ols(df, "voteinlinearg0_rcd", ballot = "prop26")
pzero_prop26 <- graf

pzero_prop26[["data"]][["days"]] <- pzero_prop26[["data"]][["days"]] * 40

pzero_prop26 <- pzero_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pzero_prop26

magr <- lm(percentage ~ days * voteinlinearg0_rcd, data = dfagr)
summary(magr)

rm(graf)



## Figure C3b.2d ----

frq(df$voteinlinearg5_rcd)

cat_ols(df, "voteinlinearg5_rcd", ballot = "prop26")
pfive_prop26 <- graf

pfive_prop26[["data"]][["days"]] <- pfive_prop26[["data"]][["days"]] * 40

pfive_prop26 <- pfive_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pfive_prop26

magr <- lm(percentage ~ days * voteinlinearg5_rcd, data = dfagr)
summary(magr)

rm(graf)





## Appendix C1 ----

df_certain_voter <- df %>% filter(filter_particip_profile == 1)

### Figure C1.2a ----
cat_ols(df_certain_voter, "party_reco_prop26_w2_know_rcd", ballot = "prop26")

pcertain01_prop26 <- graf

pcertain01_prop26[["data"]][["days"]] <- pcertain01_prop26[["data"]][["days"]] * 40

pcertain01_prop26 <- pcertain01_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain01_prop26

magr <- lm(percentage ~ days * party_reco_prop26_w2_know_rcd, data = dfagr)
summary(magr)


### Figure C1.2b ----
frq(df_certain_voter$arg_n_dk)

num_ols(df_certain_voter, "arg_n_dk", ballot = "prop26")
pcertain02_prop26 <- graf

pcertain02_prop26[["data"]][["days"]] <- pcertain02_prop26[["data"]][["days"]] * 40
pcertain02_prop26[["data"]][["avg"]] <- pcertain02_prop26[["data"]][["avg"]] * 6

pcertain02_prop26 <- pcertain02_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain02_prop26

magr <- lm(avg ~ days, data = dfagr)
summary(magr)



### Figure C1.2c ----
frq(df_certain_voter$voteinlineparty_rcd)

cat_ols(df_certain_voter, "voteinlineparty_rcd", ballot = "prop26")
pcertain04_prop26 <- graf

pcertain04_prop26[["data"]][["days"]] <- pcertain04_prop26[["data"]][["days"]] * 40

pcertain04_prop26 <- pcertain04_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain04_prop26

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)



### Figure C1.2d ----
frq(df_certain_voter$voteinlinearg_rcd)

cat_ols(df_certain_voter, "voteinlinearg_rcd", ballot = "prop26")
pcertain03_prop26 <- graf

pcertain03_prop26[["data"]][["days"]] <- pcertain03_prop26[["data"]][["days"]] * 40

pcertain03_prop26 <- pcertain03_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain03_prop26

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)

rm(graf)

### Results for figure C1.3b ----

fit_prop26certain <- sem(model, data = df_certain_voter, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_prop26_w2_know_rcd"))

sumfit_prop26certain <- summary(fit_prop26certain, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop26certain_df <- sumfit_prop26certain[["pe"]]



## Appendix C2a ----

df <- df %>% mutate(voteinlineparty_altdem = case_when(
  pid == "Democrat" & votew2 == "yes" ~ "not inline",
  pid == "Democrat" & votew2 == "no" ~ "not inline",
  pid == "Democrat" & votew2 == "dk" ~ "undecided",
  pid == "Republican" & votew2 == "yes" ~ "not inline",
  pid == "Republican" & votew2 == "no" ~ "inline",
  pid == "Republican" & votew2 == "dk" ~ "undecided",
  pid == "noPID" ~ "noPID"),
                    voteinlineparty_altdem_rcd = case_when(
                      voteinlineparty_altdem == "inline" ~ 1,
                      voteinlineparty_altdem %in% c("not inline", "undecided", "noPID") ~ 0))

frq(df$voteinlineparty_altdem)
frq(df$voteinlineparty_altdem_rcd)

### Figure C2a.2c ----
cat_ols(df, "voteinlineparty_altdem_rcd", ballot = "prop26")
paltdem04_prop26 <- graf

paltdem04_prop26[["data"]][["days"]] <- paltdem04_prop26[["data"]][["days"]] * 40

paltdem04_prop26 <- paltdem04_prop26 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 26") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

paltdem04_prop26
ggsave(filename = "prop26_dem-not-inline_agrrcs.png", units = "cm", width = 7, height = 8, dpi = 600)

magr <- lm(percentage ~ days * voteinlineparty_altdem_rcd, data = dfagr)
summary(magr)

### Figure C2a.3b ----

modelaltdem <- '
  # Days -> cue -> vote inline
  party_reco_prop26_w2_know_rcd ~ a1*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlineparty_altdem_rcd ~ c1*days + b1*party_reco_prop26_w2_know_rcd + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  
  voteinlinearg_rcd ~ c2*days + b2*arg_n_dk + isknow_prop26_scale + imp_prop26_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_prop26_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_altdem_rcd ~~ voteinlinearg_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_prop26altdem <- sem(modelaltdem, data = df, ordered = c("voteinlineparty_altdem_rcd","voteinlinearg_rcd","party_reco_prop26_w2_know_rcd"))

sumfit_prop26altdem <- summary(fit_prop26altdem, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop26altdem_df <- sumfit_prop26altdem[["pe"]]


# Empty dataframe
med_summary <- data.frame(matrix(ncol = 7, nrow = 1))
x <- c("effect", "ind", "lowind", "uppind", "dir", "lowdir", "uppdir")
colnames(med_summary) <- x

# Outcome = vote inline party cue
med_summary[1,1] <- "Proposition 26 : Time > knowledge party cue > vote inline"
med_summary[1,2] <- sumfit_prop26altdem_df[sumfit_prop26altdem_df$label == "days_cue_inline", 6]
med_summary[1,3] <- sumfit_prop26altdem_df[sumfit_prop26altdem_df$label == "days_cue_inline", 10]
med_summary[1,4] <- sumfit_prop26altdem_df[sumfit_prop26altdem_df$label == "days_cue_inline", 11]
med_summary[1,5] <- sumfit_prop26altdem_df[sumfit_prop26altdem_df$label == "c1", 6]
med_summary[1,6] <- sumfit_prop26altdem_df[sumfit_prop26altdem_df$label == "c1", 10]
med_summary[1,7] <- sumfit_prop26altdem_df[sumfit_prop26altdem_df$label == "c1", 11]

med_summary


# Indirect
ind <- med_summary %>% dplyr::select(1:4)
ind

ind$effect <- factor(ind$effect, 
                     levels = c("Proposition 26 : Time > knowledge party cue > vote inline",
                                ordered = T))

ind_plot <- ggplot(ind, aes(x = effect, y = ind)) + 
  geom_pointrange(aes(ymin = lowind, ymax = uppind), size = 0.25) +
  scale_y_continuous(limits = c(-0.06,0.45), breaks = seq(-0.05, 0.40, by = 0.10)) +
  geom_hline(yintercept = 0) +
  xlab("") + ylab("Estimate") +
  labs(title = "Indirect") +
  theme_minimal() +
  coord_flip()  

ind_plot

ggsave("prop26_dem-not-inline_mediation.png", scale = 1, width = 18, height = 4, units = "cm", dpi = 600)

#

rm(dfagr,magr,ind,ind_plot,med_summary,model,model0,model5,x,modelaltdem)




